package com.gym.model.email;

import java.util.LinkedList;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Email {
	
	private Session session;
	private final String username;
	private final String password;
	
	public Email()
	{
		//lettura dal file properties
		ResourceBundle rb = ResourceBundle.getBundle("gym.info.email");
		
		username = rb.getString("username");
		password =rb.getString("password");
		String host= rb.getString("host");
		String port = rb.getString("port");
		
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", port);
		session = Session.getInstance(props,
				  new javax.mail.Authenticator() {
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username, password);
					}
				  });
	}
	
	/**
	 * invio di una email con destinatari multipli
	 * @param to
	 * @param subject
	 * @param text
	 * @throws InvioEmailFallitoException 
	 */
	public void sendEmail(LinkedList<String> to, String subject, String text) throws InvioEmailFallitoException
	{
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(username)); //imposta il mittente
			for(String current : to)
			{
				message.addRecipients( Message.RecipientType.TO, InternetAddress.parse(current));
				 //aggiungo tutti i destinatari
			}		
			message.setSubject(subject);
			message.setText(text);
			Transport.send(message);
			System.out.println("Email.sendEmail: Done");
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new InvioEmailFallitoException("FALLITO INVIO EMAIL: "+e.getMessage());
			
		}
	}
	
	/**
	 * invio di una email con destinatario singolo
	 * @param to
	 * @param subject
	 * @param text
	 * @throws InvioEmailFallitoException 
	 */
	public void sendEmail(String to, String subject, String text) throws InvioEmailFallitoException
	{
		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(username)); //imposta il mittente
			
			message.addRecipients( Message.RecipientType.TO, InternetAddress.parse(to));	
			message.setSubject(subject);
			message.setText(text);
			Transport.send(message);
			System.out.println("Email.sendEmail: Done");
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new InvioEmailFallitoException("FALLITO INVIO EMAIL: "+e.getMessage());
			
		}
	}
	
	public void sendEmailForUser( String from, String text ) throws InvioEmailFallitoException
	{
		Message message = new MimeMessage( session );
		try {
			message.setFrom( new InternetAddress( from ) );
			message.addRecipients( Message.RecipientType.TO, InternetAddress.parse( "tmsgymproject@gmail.com" ));	
			message.setSubject( "" );
			message.setText( text );
			Transport.send(message);
			System.out.println("Email.sendEmail: Done");
		} catch (MessagingException e) {
			e.printStackTrace();
			throw new InvioEmailFallitoException("FALLITO INVIO EMAIL: "+e.getMessage());
			
		}
	}
	
	/**
	 * invio di una email precompilata di benvenuto
	 * @param to
	 * @throws InvioEmailFallitoException
	 */
	public void sendWelcomeEmail(String to) throws InvioEmailFallitoException
	{
		ResourceBundle rb = ResourceBundle.getBundle("gym.info.email");
		sendEmail(to, rb.getString("welcomeSubject"), rb.getString("welcomeText"));
	}


	public static void main(String[] args) throws InvioEmailFallitoException {
		Email mail= new Email();
		LinkedList<String> destinatari=new LinkedList<String>();
		destinatari.add("el-pibe-de-oro@hotmail.it");
		destinatari.add("alfonso.iazzetti@gmail.com");
		
		mail.sendEmail(destinatari, "prova", "invio di una email di prova");
	}
}
